/**
 * Controller class: send/receive info from back-end, front-end
 * Connect to Dudo game logic and related front-end
 * @author Ben & Long
 */
package com.dudo;

import com.dudo.model.*;

import android.app.*;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class DudoGame extends Activity implements OnClickListener {
	private static GameManager gameManager; // Back-end object
	private int curNum; // Current num of Claim
	private int curRank; // Current rank of Claim

	/** 
	 * Called when the activity is first created.
	 * @author Ben, Long 
	 */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	//start the activity from the last saved instance (if there was one)
        super.onCreate(savedInstanceState);
        
        //set the view to display (an XML from /res/layout)
        setContentView(R.layout.dudo_screen);

        // Set up a GameManger
        GameManager.initialize(this); //Bind to back-end
        gameManager = GameManager.getObject();
        gameManager.updateGame();// Start the game
        
        // Instruct the View to set up the welcome screen
        
        //grab the home button and set a listener
        Button homeButton = (Button)this.findViewById(R.id.dudo_home_button);
        homeButton.setOnClickListener(this);
        
        Button claimButton = (Button)this.findViewById(R.id.claim);
        claimButton.setOnClickListener(this);
        
        Button restartButton = (Button)this.findViewById(R.id.restart);
        restartButton.setOnClickListener(this);
    }

    /**
     * Handle different button clicks
     * @author Ben, Long
     */
	@Override
	public void onClick(View v) {
		int viewId = v.getId();
		
		switch (viewId) {
		case R.id.dudo_home_button: // Go back to home screen
			Intent intent = new Intent(this,Home.class);
			this.startActivity(intent);
			break;
		case R.id.claim: // Display a dialog, allowing user to make a claim
			showDialog(1);
			break;
		case R.id.restart:
			updateCurrentClaim("");
			GameManager.initialize(this);
			gameManager = GameManager.getObject();
	        gameManager.updateGame();// Start the game
	        break;
		}
	}
    
	/**
	 * Dialog to receive a claim from user
	 * @author Long
	 */
	@Override
	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		if (id == 1) {
			final CharSequence[] nums = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
		    builder.setTitle("Choose a number");
		    builder.setItems(nums, new DialogInterface.OnClickListener() {
		        public void onClick(DialogInterface dialog, int item) {
		        	curNum = item + 1;
		        	showDialog(2);
		        }
		    });
		} else {
			final CharSequence[] ranks = {"1", "2", "3", "4", "5", "6"};
			builder.setTitle("Choose a rank");
		    builder.setItems(ranks, new DialogInterface.OnClickListener() {
		        public void onClick(DialogInterface dialog, int item) {
		        	curRank = item + 1;
		        	gameManager.receiveClaim(curNum, curRank);
		        }
		    });
		}
		// Make a dudo claim
		builder.setPositiveButton("Dudo!", new DialogInterface.OnClickListener() {
	           public void onClick(DialogInterface dialog, int id) {
	                gameManager.receiveClaim(0, 0);
	           }
	    });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
           }
        });
        AlertDialog alert = builder.create();
        
        return alert;
	}
	
	/**
	 * Receive a claim from GameManager
	 * @author Long
	 * @param message
	 */
	public void updateCurrentClaim(String message) {
		TextView text = (TextView) this.findViewById(R.id.current_claim);
		text.setText(message);
	}
	
	/** 
	 * Receive game status from GameManager
	 * @author Long
	 * @param message
	 */
	public void updateGameStatus(String message) {
		TextView text = (TextView) this.findViewById(R.id.dudo_label_text);
		text.setText(message);
	}
}
